using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;

namespace C4F.DevKit.P2PPictureShare.PictureShareWpf
{
    // This window is responsible for displaying the tray icon, responding to
    // menu commands from the user, and initializing the p2p channel used
    // for sharing pictures.
    // Note: this Window is hidden - and a Forms user control provides
    // the tray icon and context menu functionality.
    public partial class TrayWindow : System.Windows.Window
    {
        const string TrayMessage = "Shares pictures using Windows Peer to Peer technology";
        const string PublishMessage = "Publishing shared pictures...";

        // User control that shows the tray icon, shows context menu.
        NotifyIconControl _nIcon;
        
        // Works with WCF and PeerChannel to send and receive pictures.
        PictureSharePeerChannel _peerChannel = null;

        public TrayWindow()
        {
            // VS designer generated code.
            InitializeComponent();

            // Create, display, and register for events from the notify
            // icon user control.
            InitializeTrayApp();

            this.WindowState = WindowState.Minimized;

            this.Loaded += new RoutedEventHandler(TrayWindow_Loaded);
            this.Closed += new EventHandler(TrayWindow_Closed);
        }

        void TrayWindow_Closed(object sender, EventArgs e)
        {
            Cleanup();
        }

        // Called when the user selects the Exit context menu item.
        void nIcon_ExitSelected(object sender, EventArgs e)
        {
            Cleanup();
        }

        private void Cleanup()
        {
            if (this._nIcon != null)
            {
                this._nIcon.Dispose();
            }
            if (this._peerChannel != null)
            {
                this._peerChannel.Close();
            }

            Application.Current.Shutdown();
        }

        void TrayWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Hidden;

            try
            {
                // Create and connect to the picture sharing peer channel.
                this._peerChannel = new PictureSharePeerChannel();
                this._peerChannel.Connect();
            }
            catch (InvalidOperationException ex)
            {
                MessageBox.Show(ex.Message, "Peer-to-peer Error", MessageBoxButton.OK, MessageBoxImage.Error);
                this.Close();
            }
        }

        private void InitializeTrayApp()
        {
            // Create a new notify icon user control instance.
            this._nIcon = new NotifyIconControl();

            // Register for event notifications when the user wants
            // to show the configuration window, publish pictures, or
            // exit the application.
            this._nIcon.ShowConfigWindowSelected += new EventHandler<EventArgs>(nIcon_ShowConfigWindowSelected);
            this._nIcon.PublishSelected += new EventHandler<EventArgs>(nIcon_PublishSelected);
            this._nIcon.OpenInFolder += new EventHandler<EventArgs>(_nIcon_OpenInFolder);
            this._nIcon.OpenOutFolder += new EventHandler<EventArgs>(_nIcon_OpenOutFolder);
            this._nIcon.ExitSelected += new EventHandler<EventArgs>(nIcon_ExitSelected);
            
            // Set the default message to display to the user during mouseover.
            this._nIcon.TrayText = TrayMessage;
        }

        void _nIcon_OpenOutFolder(object sender, EventArgs e)
        {
            ProcessStartInfo psi = new ProcessStartInfo("explorer.exe", SettingsWrapper.ShareFolder);
            Process.Start(psi);
        }

        void _nIcon_OpenInFolder(object sender, EventArgs e)
        {
            ProcessStartInfo psi = new ProcessStartInfo("explorer.exe", SettingsWrapper.CacheFolder);
            Process.Start(psi);
        }
              

        // Called when the user selects the Publish context menu item.
        void nIcon_PublishSelected(object sender, EventArgs e)
        {
            this._nIcon.TrayText = PublishMessage;

            // Publish the pictures in the configured share folder by sending
            // them to the configured peer channel.
            // Note: this could be updated to be perfomred on a different thread to
            // prevent blocking.
            foreach (string picFile in Directory.GetFiles(SettingsWrapper.ShareFolder))
            {
                this._peerChannel.SendPicture(picFile);
            }

            this._nIcon.TrayText = TrayMessage;
        }

        // Called when the user indicates a desire to change the configuration.
        void nIcon_ShowConfigWindowSelected(object sender, EventArgs e)
        {
            // Note: the configuration window only makes modifications to the
            // application user config file.
            ConfigurationWindow configWin = new ConfigurationWindow();
            configWin.Show();
        }
    }
}